<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./node_modules/axios/dist/axios.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      .container {
        min-height: 100vh;
        padding: 1rem;
        background-color: #f2f2f2;
      }
      main {
        min-width: 20rem;
        width: max-content;
        margin: 0 auto;
        padding: 2rem 1rem;
        background-color: #fff;
        color: #333;
        border-radius: 5px;
        box-shadow: 1px 1px 5px rgb(51, 51, 51, 0.5);
      }
      main h3 {
        margin-bottom: 0.5rem;
        text-align: center;
      }
      a {
        text-decoration: none;
      }
      #add {
        display: block;
        margin-bottom: 0.5rem;
        padding-right: 1rem;
        text-align: right;
        color: lightskyblue;
      }
      #update {
        color: lightseagreen;
      }
      #delete {
        color: lightcoral;
      }
      table {
        display: grid;
        width: 100%;
        border: 1px solid #333;
        border-radius: 5px;
      }

      tr {
        display: grid;
        grid-template-columns: 0.5fr repeat(3, 1fr);
        border-bottom: 1px solid #333;
        text-align: center;
      }
      tbody tr:first-child {
        border-top: 1px solid #333;
      }
      thead tr:last-child,
      tbody tr:last-child {
        border-bottom: none;
      }
      th,
      td {
        padding: 0.2rem 0.4rem;
        border-right: 1px solid #333;
      }
      th:last-child,
      td:last-child {
        border-right: none;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <main>
        <h3>用户列表</h3>
        <a href="./add.html" id="add">添加用户</a>
        <table>
          <thead>
            <tr>
              <th>id</th>
              <th>用户名</th>
              <th>密码</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody></tbody>
        </table>
      </main>
    </div>
    <script type="module">
      import { baseURL, pathname } from './export.js'
      axios.defaults.baseURL = baseURL + pathname
      const tbody = document.querySelector('tbody')
      axios.get('').then(res => {
        const dataArr = res.data
        tbody.innerHTML = dataArr
          .map(
            item => `
          <tr>
            <td>${item.id}</td>
            <td>${item.username}</td>
            <td>${item.password}</td>
            <td><a href="./update.html?id=${item.id}" id="update">修改</a>&nbsp;<a href="" id="delete">删除</a></td>
          </tr>`
          )
          .join('')
      })
      tbody.addEventListener('click', function (e) {
        if (e.target.id != 'delete') return
        e.preventDefault()
        const flag = confirm('确定删除？')
        if (flag != true) return
        const deleteBtn = e.target
        const id = deleteBtn.parentElement.parentElement.firstElementChild.textContent
        deleteBtn.parentElement.parentElement.parentElement.removeChild(
          deleteBtn.parentElement.parentElement
        )
        axios.delete('/' + id)
      })
    </script>
  </body>
</html>
